package com.echo.neocommunity.controller;

import com.echo.neocommunity.event.EventProducer;
import com.echo.neocommunity.pojo.Event;
import com.echo.neocommunity.pojo.User;
import com.echo.neocommunity.service.impl.LikeService;
import com.echo.neocommunity.utils.CommunityConstant;
import com.echo.neocommunity.utils.HostHolder;
import com.echo.neocommunity.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LikeController {

    @Autowired
    private LikeService likeService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer producer;

    @ResponseBody
    @PostMapping("/like")
    public R like(int entityType,int entityId,int  entityUserId,int postId){
        User user = hostHolder.getUser();
        likeService.like(user.getId(),entityType,entityId,entityUserId);
        Long likeCount = likeService.getLikeCount(entityType, entityId);
        int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);
        if(likeStatus==1){
            //点赞
            Event event=new Event();
            event.setTopic(CommunityConstant.TOPIC_LIKE);
            event.setUserId(user.getId());
            event.setEntityType(entityType);
            event.setEntityId(entityId);
            event.setEntityUserId(entityUserId);
            event.setData("postId",postId);
            producer.fireEvent(event);
        }
        return R.ok().put("likeCount",likeCount).put("likeStatus",likeStatus);
    }

}
